package net.wanglu.www.zzz.rule.sva;

import java.util.HashMap;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.nsubj._NSubjTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeGraphNode;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class RestRemainderLastAsSubj extends _SvaRule {

	public static final TregexPattern TheRest = TregexPattern.compile("/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\brest\\b/)");
	public static final TregexPattern TheRestOfNN = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\brest\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bNN\\b/))");
	public static final TregexPattern TheRestOfNNS = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\brest\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bNNS\\b/))");
	public static final TregexPattern TheRestOfPRP = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\brest\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bPPR\\b/))");

	public static final TregexPattern TheRemainder = TregexPattern.compile("/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\bremainder\\b/)");
	public static final TregexPattern TheRemainderOfNN = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\bremainder\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bNN\\b/))");
	public static final TregexPattern TheRemainderOfNNS = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\bremainder\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bNNS\\b/))");
	public static final TregexPattern TheRemainderOfPRP = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bNN\\b/</\\bremainder\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bPPR\\b/))");

	public static final TregexPattern TheLastOfNN = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bJJ\\b/</\\blast\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bNN\\b/))");
	public static final TregexPattern TheLastOfNNS = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bJJ\\b/</\\blast\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bNNS\\b/))");
	public static final TregexPattern TheLastOfPRP = TregexPattern.compile("/\\bNP\\b/<(/\\bNP\\b/<(/\\bDT\\b/</\\bThe\\b/)<(/\\bJJ\\b/</\\blast\\b/))<(/\\bPP\\b/<(/\\bIN\\b/</\\bof\\b/)<(/\\bNP\\b/</\\bPPR\\b/))");

	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( super.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		_NSubjTuple nst = (_NSubjTuple) objects[1];
		if ( !"NP".equals(nst.getSubjectAncestor().value())) return Rule.Inapplicable;
		return Rule.Applicable;
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		_NSubjTuple nst =  (_NSubjTuple) objects[1];
		
		if ( this.applicable(preprocessData, nst) == Rule.Inapplicable) return Rule.Inapplicable;
		TreeGraphNode subj = (TreeGraphNode) nst.getSubj();
		TreeGraphNode verb = (TreeGraphNode) nst.getVerb();
		TreeGraphNode cop = (TreeGraphNode) nst.getCop();
		TreeGraphNode closestAux = (TreeGraphNode) nst.getAux();
		Tree subjAncestor = nst.getSubjectAncestor();
		
		TreeGraphNode pred = verb;
		if ( cop != null) pred = cop;
		if ( closestAux != null) pred = closestAux;
		String predWord = pred.label().word();
		String predTag = pred.label().tag();

		sb.clear();
		for( TaggedWord tw : subjAncestor.taggedYield()) {
			sb.append(tw.word()).append(" ");
		}
		
		if ( RestRemainderLastAsSubj.TheRestOfPRP.matcher(subjAncestor).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		} else if ( RestRemainderLastAsSubj.TheRestOfNNS.matcher(subjAncestor).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		} else if ( RestRemainderLastAsSubj.TheRestOfNN.matcher(subjAncestor).find()) {
			if (Rule.SingularVerb.matcher(predTag).find() && !"were".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}						
		} else if ( RestRemainderLastAsSubj.TheRest.matcher(subjAncestor).find()) {
			if (Rule.SingularVerb.matcher(predTag).find() && !"were".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}						
		} else if ( RestRemainderLastAsSubj.TheRemainderOfPRP.matcher(subjAncestor).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		} else if ( RestRemainderLastAsSubj.TheRemainderOfNNS.matcher(subjAncestor).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		} else if ( RestRemainderLastAsSubj.TheRemainderOfNN.matcher(subjAncestor).find()) {
			if (Rule.SingularVerb.matcher(predTag).find() && !"were".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}						
		} else if ( RestRemainderLastAsSubj.TheRemainder.matcher(subjAncestor).find()) {
			if (Rule.SingularVerb.matcher(predTag).find() && !"were".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}						
		}  else if ( RestRemainderLastAsSubj.TheLastOfPRP.matcher(subjAncestor).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		} else if ( RestRemainderLastAsSubj.TheLastOfNNS.matcher(subjAncestor).find()) {
			if (Rule.PluralVerb.matcher(predTag).find() && !"was".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}
		} else if ( RestRemainderLastAsSubj.TheLastOfNN.matcher(subjAncestor).find()) {
			if (Rule.SingularVerb.matcher(predTag).find() && !"were".equals(predWord)) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				egg.whereIsWrong = nst;
				return egg;
			}						
		}
		return Rule.Inapplicable;
	}
	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.clear();
		sb.append("<p>");
		sb.append("当rest，reminder，last等作主语时，应遵循概念一致的原则，如果表示的复数概念，谓语动词用复数形式，如果表示单数概念，谓语动词用单数形式。");
		sb.append("</p>");
		return sb.toString();
	}
}
